package fr.umlv.m2.jee.spij.persistence.education;

import fr.umlv.m2.jee.spij.persistence.actor.StudentLesson;
import fr.umlv.m2.jee.spij.persistence.actor.TeacherLesson;
import org.hibernate.annotations.Type;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Entity Lesson who contain information about lesson.
 *
 * @author Lounes Akroune
 * @author Aurelien Scelles
 */
@Entity
@Table(name = "LES_LESSON")
public class Lesson extends Education {

    /**
     * Primary key.
     */
    @Id
    @Column(name = "LES_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    /**
     * Contain of lesson.
     */
    @Lob
    @Column(name = "LES_CONTAIN", nullable = false)
    private String contain;

    /**
     * Link of lesson in PDF.
     */
    @Column(name = "LES_LINK_SUPPORT")
    private String linkSupport;

    /**
     * Id of linked unit.
     */
    @Column(name = "LES_UNI_ID")
    private Long unit;

    /**
     * List of students
     */
    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "SL_LESSON_ID", referencedColumnName = "LES_ID")
    private List<StudentLesson> students = new ArrayList<StudentLesson>();

    /**
     * List of teachers
     */
    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "TL_LESSON_ID", referencedColumnName = "LES_ID")
    private List<TeacherLesson> teachers = new ArrayList<TeacherLesson>();

    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Type(type = "org.hibernate.type.PrimitiveByteArrayBlobType")
    @Column(name = "LES_FILE_DATA", columnDefinition = "LONGBLOB")
    private byte[] fileData;

    /**
     * Setter of Contain.
     *
     * @param contain
     */
    public void setContain(String contain) {
        this.contain = contain;
    }

    /**
     * Getter of Contain.
     *
     * @return contain of lesson
     */
    public String getContain() {
        return contain;
    }

    /**
     * Setter of LinkSupport.
     *
     * @param linkSupport
     */
    public void setLinkSupport(String linkSupport) {
        this.linkSupport = linkSupport;
    }

    /**
     * Getter of LinkSupport.
     *
     * @return link of lesson
     */
    public String getLinkSupport() {
        return linkSupport;
    }

    /**
     * Setter of Id.
     *
     * @param id
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * Getter of Id.
     *
     * @return primary key.
     */
    public long getId() {
        return id;
    }

    /**
     * Setter of linked Unit.
     *
     * @param unit
     */
    public void setUnit(Long unit) {
        this.unit = unit;
    }

    /**
     * Getter of linked Unit.
     *
     * @return unit id
     */
    public Long getUnit() {
        return unit;
    }

    public void setStudents(List<StudentLesson> students) {
        this.students = students;
    }

    public List<StudentLesson> getStudents() {
        return students;
    }

    /**
     * @param arg0
     *
     * @return
     *
     * @see java.util.List#add(java.lang.Object)
     */
    public boolean addStudent(StudentLesson arg0) {
        return students.add(arg0);
    }

    /**
     * @param arg0
     *
     * @return
     *
     * @see java.util.List#get(int)
     */
    public StudentLesson getStudent(int arg0) {
        return students.get(arg0);
    }

    /**
     * @param arg0
     *
     * @return
     *
     * @see java.util.List#remove(java.lang.Object)
     */
    public boolean removeStudent(Object arg0) {
        return students.remove(arg0);
    }

    /**
     * @return
     *
     * @see java.util.List#size()
     */
    public int numberOfStudents() {
        return students.size();
    }

    public void setTeachers(List<TeacherLesson> teachers) {
        this.teachers = teachers;
    }

    public List<TeacherLesson> getTeachers() {
        return teachers;
    }

    /**
     * @param e
     *
     * @return
     *
     * @see java.util.List#add(java.lang.Object)
     */
    public boolean addTeacher(TeacherLesson e) {
        return teachers.add(e);
    }

    /**
     * @param index
     *
     * @return
     *
     * @see java.util.List#get(int)
     */
    public TeacherLesson getTeacher(int index) {
        return teachers.get(index);
    }

    /**
     * @param o
     *
     * @return
     *
     * @see java.util.List#remove(java.lang.Object)
     */
    public boolean removeTeacher(Object o) {
        return teachers.remove(o);
    }

    /**
     * @return
     *
     * @see java.util.List#size()
     */
    public int numberOfTeachers() {
        return teachers.size();
    }

    /**
     * @param fileData the fileData to set
     */
    public void setFileData(byte[] fileData) {
        this.fileData = Arrays.copyOf(fileData, fileData.length);
    }

    /**
     * @return the fileData
     */
    public byte[] getFileData() {
        return Arrays.copyOf(fileData, fileData.length);
    }

}
